package defectsim.agents;

import java.awt.Color;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;

import repast.simphony.visualization.visualization2D.style.Style2D;
import edu.umd.cs.piccolo.PNode;
import edu.umd.cs.piccolo.nodes.PPath;
import edu.umd.cs.piccolo.nodes.PText;

/**
 * The 2D style class for defect agents.   
 * The style is a triangle. 
 *
 * @author Eric Tatara
 * @author Nick Collier
 * @author Altug Uzunali
 * @version 
 */

public class DefectStyle2D implements Style2D<DefectAgent> {

	Rectangle2D rect = new Rectangle2D.Float(0, 0, 2, 2);
	Shape circle = new Ellipse2D.Float(0, 0, 1, 1);

	Stroke stroke = null;

	double rotation = 0.0;

	public DefectStyle2D() {
		super();
	}

	// Sets how the node is initially drawn (for new agents)
	public PNode getPNode(DefectAgent agent, PNode node) {
		
		PPath path = null;
		if (agent instanceof Dislocation){
			GeneralPath triangle = new GeneralPath();
			Dislocation d = (Dislocation)agent;
			if (d.getUnitBurgersVector() > 0) {
				triangle.moveTo(0, 0);  
				triangle.lineTo(2, 1);  
				triangle.lineTo(0, 2);  
				triangle.lineTo(0, 0);  
			}
			else {
				triangle.moveTo(2, 0);  
				triangle.lineTo(0, 1);  
				triangle.lineTo(2, 2);  
				triangle.lineTo(2, 0);  
			}				
			triangle.closePath();
			path = new PPath(triangle);
		}
		if (agent instanceof PointDefect){
			path = new PPath(circle);
		}
		 
		path.setBounds(rect);
		path.setPaint(this.getPaint(agent));
		path.setStroke(this.getStroke(agent));
		path.setStrokePaint(this.getStrokePaint(agent));

		return path;
	}

	public Paint getPaint(DefectAgent agent) {
		Color c = null;
		if (agent instanceof EdgeDislocation){
			EdgeDislocation d = (EdgeDislocation)agent;
			double bg = d.getUnitBurgersVector();
			double dir = d.getDirection();
			if (bg < 0)
				dir += 180;
//			this.setRotation(Math.toRadians(dir));
			if(d.isDipole())
				c = Color.ORANGE;
			else 
				if (d.isMobile())
					c = Color.GRAY;
				else
					if (d.isPinned())
						c = Color.BLACK;
					else
						c = Color.RED;
		}
		else 
			if (agent instanceof PointDefect)
				c = Color.CYAN;
		
		return c;
	}
	public double getRotation(DefectAgent agent) {
		return rotation;
	}
	public void setRotation(double rot) {
		this.rotation = rot;
	}
	public void setBounds(Rectangle2D bounds) {
		this.rect = bounds;
	}
	public Rectangle2D getBounds(DefectAgent agent) {
		return rect;
	}
	public Stroke getStroke(DefectAgent agent) {
		return stroke;
	}
	public boolean isScaled(DefectAgent agent) {
		return true;
	}
	public Paint getStrokePaint(DefectAgent agent) {
		return Color.RED;
	}

	public PText getLabel(DefectAgent object) {
		// TODO Auto-generated method stub
		return null;
	}
}

